//
//  UIButton+WDExtention.m
//  MyDemos
//
//  Created by iMac on 2018/3/13.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "UIButton+WDExtention.h"
#import <objc/runtime.h>


const char * tapButtonCallbackKey = "tapButtonCallbackKey";
@implementation UIButton (WDExtention)

/// set title&color nomal
- (UIButton *(^)(NSString *, UIColor *))sNomalTitleColor {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *title, UIColor *color) {
        [weekSelf setTitle:title forState:UIControlStateNormal];
        [weekSelf setTitleColor:color forState:UIControlStateNormal];
        return weekSelf;
    };
}
/// set title nomal
- (UIButton *(^)(NSString *))sNomalTitle {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *title) {
        [weekSelf setTitle:title forState:UIControlStateNormal];
        return weekSelf;
    };
}
/// set title nomal
- (UIButton *(^)(UIColor *))sNomalColor {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(UIColor *color) {
        [weekSelf setTitleColor:color forState:UIControlStateNormal];
        return weekSelf;
    };
}



/// set title&color selected
- (UIButton *(^)(NSString *, UIColor *))sSelectedTitleColor {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *title, UIColor *color) {
        [weekSelf setTitle:title forState:UIControlStateSelected];
        [weekSelf setTitleColor:color forState:UIControlStateSelected];
        return weekSelf;
    };
}
/// set title selected
- (UIButton *(^)(NSString *))sSelectedTitle {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *title) {
        [weekSelf setTitle:title forState:UIControlStateSelected];
        return weekSelf;
    };
}
/// set color selected
- (UIButton *(^)(UIColor *))sSelectedColor {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(UIColor *color) {
        [weekSelf setTitleColor:color forState:UIControlStateSelected];
        return weekSelf;
    };
}




/// set title&color highlighted
- (UIButton *(^)(NSString *, UIColor *))sHighlightedTitleColor {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *title, UIColor *color) {
        [weekSelf setTitle:title forState:UIControlStateHighlighted];
        [weekSelf setTitleColor:color forState:UIControlStateHighlighted];
        return weekSelf;
    };
}
/// set title highlighted
- (UIButton *(^)(NSString *))sHighlightedTitle {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *title) {
        [weekSelf setTitle:title forState:UIControlStateHighlighted];
        return weekSelf;
    };
}
/// set color highlighted
- (UIButton *(^)(UIColor *))sHighlightedColor {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(UIColor *color) {
        [weekSelf setTitleColor:color forState:UIControlStateHighlighted];
        return weekSelf;
    };
}




/// set image normal
- (UIButton *(^)(NSString *))sNomalImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *image) {
        [weekSelf setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        return weekSelf;
    };
}

/// set image selected
- (UIButton *(^)(NSString *))sSelectedImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *image) {
        [weekSelf setImage:[UIImage imageNamed:image] forState:UIControlStateSelected];
        return weekSelf;
    };
}

/// set image highlighted
- (UIButton *(^)(NSString *))sHighlightedImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *image) {
        [weekSelf setImage:[UIImage imageNamed:image] forState:UIControlStateHighlighted];
        return weekSelf;
    };
}





/// set backgroundColor
- (UIButton *(^)(UIColor *))sBGColor {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(UIColor *color) {
        [weekSelf setBackgroundColor:color];
        return weekSelf;
    };
}



/// set backgroundImage nomal
- (UIButton *(^)(NSString *))sNomalBGImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *image) {
        [weekSelf setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        return weekSelf;
    };
}
/// set backgroundImage selected
- (UIButton *(^)(NSString *))sSelectedBGImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *image) {
        [weekSelf setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateSelected];
        return weekSelf;
    };
}
/// set backgroundImage highlighted
- (UIButton *(^)(NSString *))sHighlightedBGImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSString *image) {
        [weekSelf setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateHighlighted];
        return weekSelf;
    };
}




/// set backgroundImage(color) nomal
- (UIButton *(^)(UIColor *))sNomalBGColorImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(UIColor *color) {
        [weekSelf setBackgroundImage:[weekSelf wdColorImage:color] forState:UIControlStateNormal];
        return weekSelf;
    };
}
/// set backgroundImage(color) selected
- (UIButton *(^)(UIColor *))sSelectedBGColorImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(UIColor *color) {
        [weekSelf setBackgroundImage:[weekSelf wdColorImage:color] forState:UIControlStateSelected];
        return weekSelf;
    };
}
/// set backgroundImage(color) highlighted
- (UIButton *(^)(UIColor *))sHighlightedBGColorImage {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(UIColor *color) {
        [weekSelf setBackgroundImage:[weekSelf wdColorImage:color] forState:UIControlStateHighlighted];
        return weekSelf;
    };
}




/// set frame
- (UIButton *(^)(CGRect))sFrame {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(CGRect frame) {
        [weekSelf setFrame:frame];
        return weekSelf;
    };
}
/// set tag
- (UIButton *(^)(NSInteger))sTag {
    __weak typeof(self)weekSelf = self;
    return ^UIButton *(NSInteger tag) {
        [weekSelf setTag:tag];
        return weekSelf;
    };
}
/// set sys font size
- (UIButton *(^)(CGFloat))sSysFont {
    __weak typeof(self)weekSelf = self;
    return ^UIButton * (CGFloat size) {
        weekSelf.titleLabel.font = [UIFont systemFontOfSize:size];
        return weekSelf;
    };
}
/// set sys font size
- (UIButton *(^)(CGFloat, NSString *))sSysFontName {
    __weak typeof(self)weekSelf = self;
    return ^UIButton * (CGFloat size, NSString *name) {
        weekSelf.titleLabel.font = [UIFont fontWithName:name size:size];
        return weekSelf;
    };
}



/// button click
- (UIButton *)touchupInset:(nullable id)target action:(nonnull SEL)selctor {
    [self addTarget:target action:selctor forControlEvents:UIControlEventTouchUpInside];
    return self;
}
- (UIButton *)valueChanged:(nullable id)target action:(nonnull SEL)selctor {
    [self addTarget:target action:selctor forControlEvents:UIControlEventValueChanged];
    return self;
}


/// button click block
- (UIButton *)tap:(void (^)(UIButton *))callBack {
    self.callBack = callBack;
    [self addTarget:self action:@selector(callBackSelector:) forControlEvents:UIControlEventTouchUpInside];
    return self;
}
- (UIButton *)valueChange:(void (^)(UIButton *))callBack {
    self.callBack = callBack;
    [self addTarget:self action:@selector(callBackSelector:) forControlEvents:UIControlEventValueChanged];
    return self;
}
- (void)setCallBack:(void (^)(UIButton *))callBack {
    objc_setAssociatedObject(self, tapButtonCallbackKey, callBack, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(UIButton *))callBack {
    return objc_getAssociatedObject(self, tapButtonCallbackKey);
}
- (void)callBackSelector:(UIButton *)sender {
    if (self.callBack) {
        self.callBack(sender);
    }
}



- (UIImage *)wdColorImage:(UIColor *)color {
    CGSize size = CGSizeMake(1, 1);
    UIGraphicsBeginImageContext(size);
    CGContextRef contex = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(contex, color.CGColor);
    CGContextFillRect(contex, CGRectMake(0, 0, size.width, size.height));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
@end
